﻿namespace LinFu.AOP.Interfaces
{
    using System;

    public class SimpleAroundInvokeProvider : IAroundInvokeProvider
    {
        #region Fields

        private IAroundInvoke _around;
        private Predicate<IInvocationContext> _predicate;

        #endregion Fields

        #region Constructors

        public SimpleAroundInvokeProvider(IAroundInvoke around)
        {
            _around = around;
        }

        public SimpleAroundInvokeProvider(IAroundInvoke around, Predicate<IInvocationContext> predicate)
        {
            _around = around;
            _predicate = predicate;
        }

        #endregion Constructors

        #region Properties

        public Type IAroundInvokeType
        {
            get; set;
        }

        public Predicate<IInvocationContext> Predicate
        {
            get { return _predicate;  }
            set { _predicate = value; }
        }

        #endregion Properties

        #region Methods

        public IAroundInvoke GetSurroundingImplementation(IInvocationContext context)
        {
            if (_predicate == null)
                return _around;

            // Apply the predicate, if possible
            if (_predicate(context))
                return _around;

            return null;
        }

        #endregion Methods
    }
}